import {defineStore} from 'pinia'

export const useUserStore = defineStore('user', {
    state: () => ({
        token: null,
        userrole: null
    }),
    actions: {
        setToken(token){
            this.token = token;
            localStorage.removeItem('token');
            localStorage.setItem('token', this.token)
        },
        removeToken(){
            this.token = null;
            this.userinfo = null;
            localStorage.removeItem('token')
        },
        setUserrole(userrole){
            this.userrole = userrole;
            localStorage.setItem('userrole', this.userrole)
        },
    },

    getters: {
        getToken: (store) => {

            if(localStorage.getItem('token')){
                store.token = localStorage.getItem('token')
            }

            return store.token;
        },
        getUserrole: (store) => {
            if(localStorage.getItem("userrole")){
                store.userrole = localStorage.getItem("userrole")
                return localStorage.getItem("userrole")
            }
            return store.userrole;
        }
    }
});

